package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.Banner;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Controller
public class BannerController {

    @Autowired
    BannerService bannerService;

    @ResponseBody
    @GetMapping("/getBannerList")
    public JSONObject getBannerList() {
        JSONObject jsonObject = new JSONObject();

        List<Banner> bannerList = bannerService.queryBannerList();

        jsonObject.put("bannerList", bannerList);

        return jsonObject;
    }

    @GetMapping("/bannerManagement")
    public String getBannerManagement() {
        return "/bannerManagement";
    }

    @ResponseBody
    @PostMapping("/toBannerManagement")
    public JSONObject bannerManagement(int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<Banner> bannerPage = bannerService.queryBannerByPageNoAndPageSize(page, 10);

        for (Banner banner : bannerPage.getItems()) {
            System.out.println(banner);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", bannerPage.getPageCountTotal());
        jsonObject.put("data", bannerPage.getItems());

        return jsonObject;
    }

    @ResponseBody
    @PostMapping("/deleteBanner")
    public void deleteBanner(int id) {
        bannerService.deleteBanner(id);
    }

    @ResponseBody
    @PostMapping("/addBanner")
    public void addBanner(MultipartFile file) {

        //最终存入数据库的图片地址
        String filepath = null;

        if (!file.isEmpty()) {
            try {
                // 时间戳
                String[] st = file.getOriginalFilename().split("\\.");
                int suffixIndex = st.length - 1;
                String exe = st[suffixIndex];
                Long timeStamp = System.currentTimeMillis();

                System.out.println("上传的文件名：" + file.getOriginalFilename());

                String fileName = timeStamp + "." + exe;
                String filePath = "e:\\test\\" + fileName;

                System.out.println("修改的文件名：" + filePath);
                System.out.println("/ImagineCommunity/test/" + fileName);

                filepath = "/ImagineCommunity/test/" + fileName;

                File saveFile = new File(filePath);
                if (!saveFile.getParentFile().exists())
                    saveFile.getParentFile().mkdirs();
                file.transferTo(saveFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        bannerService.addBanner(new Banner(null, filepath));
    }

    /**
     * 批量删除
     *
     * @param idList
     */
    @ResponseBody
    @PostMapping("/deleteBannerList")
    public void deleteBannerList(String idList) {

        String[] id = idList.split(",");

        for (int i = 0; i < id.length; i++) {
            if (!"".equals(id[i])) {
                int idInt = Integer.parseInt(id[i]);
                bannerService.deleteBanner(idInt);
            }
        }

    }

}
